limsSample.managePage = {

    getGridParam: function () {
        var _this = this;
        return {
            type: 'grid',
            id: 'lims-sample-manage-list-grid',
            url: limsSample.baseUrl + '/queries/rows',
            requestData: {
                orgFilter : '1'
            },
            page: true,
            pageList: [50, 100, 200],
            filterOpen: true,
            service: 'limsSampleServiceImpl',
            columns: limsSample.getManageListColumns(),
            onCellClick: function (field, row) {
                field === 'ext$.batchcode' && row.ext$.batchid && Gikam.loadBatchDetailPageV12(row.ext$.batchid, true);
            },
            onLoadSuccess: function (data) {
                if (!Gikam.isEmpty(data)) {
                    this.activeRowByIndex(0);
                } else {
                    Gikam.cleanCompData(['sample-enter-info-grid', 'sample-out-info-grid', 'sample-destroy-info-grid', 'sample-back-info-grid','location-change-list-grid']);
                    Gikam.getAsyncComp('sample-detail-uploader').done(function (comp) {
                        comp.setOptions({
                            bizId: -1,
                        });
                    });
                }
            },
            onRowActive: function (index, row) {
                Gikam.getAsyncComp('sample-enter-info-grid').done(function (grid) {
                    grid.refresh({
                        url: limsSample.sampleHandleUrl + '/queries',
                        requestData: {
                            sampleId: row.id,
                            handleType_EQ: 'enter'
                        }
                    });
                });
                Gikam.getAsyncComp('sample-out-info-grid').done(function (grid) {
                    grid.refresh({
                        url: limsSample.sampleHandleUrl + '/queries',
                        requestData: {
                            sampleId: row.id,
                            handleType_EQ: 'out'
                        }
                    });
                });
                Gikam.getAsyncComp('sample-destroy-info-grid').done(function (grid) {
                    grid.refresh({
                        url: limsSample.sampleHandleUrl + '/queries',
                        requestData: {
                            sampleId: row.id,
                            handleType_EQ: 'destroy'
                        }
                    });
                });
                Gikam.getAsyncComp('sample-back-info-grid').done(function (grid) {
                    grid.refresh({
                        url: limsSample.sampleHandleUrl + '/queries',
                        requestData: {
                            sampleId: row.id,
                            handleType_EQ: 'back'
                        }
                    });
                });
                Gikam.getAsyncComp('sample-detail-uploader').done(function (comp) {
                    comp.setOptions({
                        bizId: row.id,
                    });
                });
                Gikam.getAsyncComp('location-change-list-grid').done(function (grid) {
                    grid.refresh({
                        url : limsSample.locationChangeUrl + '/queries',
                        requestData : {
                            businessId_EQ: row.id,
                            businessCode_SEQ : 'T_LIMS_SAMPLE'
                        }
                    })
                });
            },
            toolbar: [{
                type: 'button',
                text: 'GIKAM.TITLE.CHOOSE.SEARCH.SAMPLECODE',
                icon: 'select',
                onClick: function () {
                    Gikam.create('modal', {
                        title: 'GIKAM.TITLE.CHOOSE.SEARCH',
                        height: 400,
                        width: 400,
                        url: limsSample.baseUrl + '/page/sample-select' + Gikam.param({
                            type: 'sample'
                        }),
                        onAfterClose: function (data) {
                            if (Gikam.isEmpty(data)) {
                                return;
                            }
                            let items = data.sampleCode.replace(/[(\r\n)|(\ +)|(，)|(；)|(;)]/g, ',');
                            Gikam.post(limsSample.baseUrl + '/queries', Gikam.getJsonWrapper({
                                f: {
                                    sampleCode_IN: items
                                }
                            })).done(function (data) {
                                Gikam.getComp('lims-sample-manage-list-grid').loadData(data.rows);
                            })
                        }
                    })
                }
            }, {
                type: 'button',
                text: 'GIKAM.BUTTON.ASSIGN',
                icon: 'modify',
                onClick: function () {
                    var grid = Gikam.getComp('lims-sample-manage-list-grid');
                    var selections = grid.getSelections();
                    if (Gikam.isEmpty(selections)) {
                        Gikam.alert('GIKAM.TIP.CHOOSE_AT_LEAST_ONE_ITEM');
                        return;
                    }
                    let ids = selections.map(function (item) {
                        return item.id
                    }).join(',');
                    Gikam.create('modal', {
                        width: '90%',
                        height: '90%',
                        title: Gikam.choose.getConfig('sample-board-choose').title,
                        url: IFM_CONTEXT + Gikam.choose.getConfig("sample-board-choose").url + Gikam.param({
                            id_IN: ids
                        }),
                        onAfterClose: function () {
                            Gikam.getComp('lims-sample-manage-list-grid').refresh();
                        }
                    });
                }
            },{
                type: 'button',
                text: 'GIKAM.BUTTON.KSH.ASSIGN',
                icon: 'modify',
                onClick: function () {
                    var grid = Gikam.getComp('lims-sample-manage-list-grid');
                    var selections = grid.getSelections();
                    if (Gikam.isEmpty(selections)) {
                        Gikam.alert('GIKAM.TIP.CHOOSE_AT_LEAST_ONE_ITEM');
                        return;
                    }
                    let ids = selections.map(function (item) {
                        return item.id
                    }).join(',');
                    Gikam.create('modal', {
                        width: '90%',
                        height: '90%',
                        title: Gikam.choose.getConfig('ksh2-sample-board-choose').title,
                        url: IFM_CONTEXT + Gikam.choose.getConfig("ksh2-sample-board-choose").url + Gikam.param({
                            id_IN: ids
                        }),
                        onAfterClose: function () {
                            Gikam.getComp('lims-sample-manage-list-grid').refresh();
                        }
                    });
                }
            }, {
                type: 'button',
                text: 'T_LIMS_SAMPLE.BUTTON.VALIDITY_SETTING',
                icon: 'modify',
                onClick: function () {
                    var grid = Gikam.getComp('lims-sample-manage-list-grid');
                    var selections = grid.getSelections();
                    if (Gikam.isEmpty(selections)) {
                        Gikam.alert('GIKAM.TIP.CHOOSE_AT_LEAST_ONE_ITEM');
                        return;
                    }
                    Gikam.create('modal', {
                        title: 'T_LIMS_SAMPLE.BUTTON.VALIDITY_SETTING',
                        width: 400,
                        height: 220,
                    });
                    Gikam.create('layout', {
                        renderTo: Gikam.getLastModal().window.$dom,
                        center: {
                            items: [{
                                type: 'form',
                                id: 'set-form',
                                columns: 1,
                                fields: [{
                                    field: 'expiredTime',
                                    title: 'T_LIMS_SAMPLE.EXPIREDTIME',
                                    type: 'dateTime',
                                    validators: ['notEmpty'],
                                }, {
                                    field: 'expireReminderTime',
                                    title: 'T_LIMS_SAMPLE.EXPIREDREMINDERTIME',
                                    type: 'dateTime',
                                    validators: ['notEmpty'],
                                }]
                            }, {
                                type: 'btnToolbar',
                                items: [{
                                    type: 'button',
                                    text: 'GIKAM.BUTTON.BINDING',
                                    icon: 'select',
                                    onClick: function () {
                                        let form = Gikam.getComp('set-form');
                                        if (form.validate()) {
                                            var formData = form.getData()
                                            var expiredTime = new Date(formData.expiredTime)
                                            var expireReminderTime = new Date(formData.expireReminderTime)
                                            if (expireReminderTime > expiredTime) {
                                                Gikam.alert(Gikam.propI18N('T_LIMS_SAMPLE.EXPIREDTIME') + '不得大于' + Gikam.propI18N('T_LIMS_SAMPLE.EXPIREDREMINDERTIME'))
                                            } else {
                                                var chooseData = selections.map(function (item) {
                                                    return {
                                                        id: item.id,
                                                        expiredTime: form.getData().expiredTime,
                                                        expireReminderTime: form.getData().expireReminderTime
                                                    }
                                                });
                                                workspace.window.showMask();
                                                Gikam.put(limsSample.baseUrl + '/instant', JSON.stringify({
                                                    b: ['limsSampleServiceImpl', chooseData]
                                                })).done(function (data) {
                                                    grid.refresh();
                                                }).always(function () {
                                                    workspace.window.closeMask();
                                                    Gikam.getLastModal().close();
                                                });
                                            }
                                        }
                                    }
                                }, {
                                    type: 'button',
                                    text: 'GIKAM.BUTTON.CANCEL',
                                    icon: 'back',
                                    onClick: function () {
                                        Gikam.getLastModal().close();
                                    }
                                }]
                            }]
                        }
                    });
                }
            }, {
                type: 'button',
                text: 'GIKAM.BUTTON.LABEL-PRINT',
                icon: 'print',
                onClick: function () {
                    var mainGrid = Gikam.getComp('lims-sample-manage-list-grid');
                    var selections = mainGrid.getSelections();
                    if (Gikam.isEmpty(selections)) {
                        Gikam.alert('GIKAM.TIP.CHOOSE_AT_LEAST_ONE_ITEM');
                        return
                    }
                    workspace.window.showMask();
                    Gikam.postText(IFM_CONTEXT + '/secure/baseline/samplemanage/samples' + '/print', Gikam.getJsonWrapper(null, ['limsSampleServiceImpl', selections])).done(function (fileId) {
                        Gikam.openFile(fileId, false)
                    }).always(function () {
                        workspace.window.closeMask()
                    })
                }
            }]
        }
    },

    getSubTabParam: function () {
        var _this = this;
        return {
            type: 'tab',
            fill: true,
            panels: [{
                title: 'BASEMODULE.SAMPLEMANAGE.SAMPLES.APPLY.TAB.INFO_GRID',
                items: [{
                    id: 'sample-enter-info-grid',
                    type: 'grid',
                    columns: limsSample.gethandleListColumns(),
                }]
            }, {
                title: 'BASEMODULE.SAMPLEMANAGE.SAMPLES.OUT.TAB.INFO_GRID',
                items: [{
                    id: 'sample-out-info-grid',
                    type: 'grid',
                    columns: limsSample.gethandleListColumns(),
                }]
            }, {
                title: 'BASEMODULE.SAMPLEMANAGE.SAMPLES.DESTROY.INFO_GRID',
                items: [{
                    id: 'sample-destroy-info-grid',
                    type: 'grid',
                    columns: limsSample.gethandleListColumns(),
                }]
            }, {
                title: 'BASEMODULE.SAMPLEMANAGE.SAMPLES.BACK.INFO_GRID',
                items: [{
                    id: 'sample-back-info-grid',
                    type: 'grid',
                    columns: limsSample.getBackHandleListColumns(false),
                }]
            }, {
                title: 'GIKAM.FILE.ATTACHMENT',
                items: [{
                    type: 'uploader',
                    id: 'sample-detail-uploader',
                    dbTable: 'T_LIMS_SAMPLE',
                    readonly: true,
                    formatterGridColumns: function (columns) {
                        return Gikam.getFormatterGridColumns(columns);
                    }
                }]
            },{
                title : 'T_LIMS_LOCATION_CHANGE.SERVICE_NAME',
                items : [{
                    id : 'location-change-list-grid',
                    type : 'grid',
                    order : {createdTime : 'desc'},
                    columns : limsSample.getLocationChangeColumns()
                }]
            }]
        }
    },

    create: function () {
        var _this = this;
        Gikam.create('layout', {
            id: 'lims-sample-manage-list-layout',
            renderTo: workspace.window.$dom,
            north: {
                height: '60%',
                items: [this.getGridParam()]
            },
            center: {
                items: [this.getSubTabParam()]
            }
        });
    },

    init: function () {
        this.create();
    }
};
